package com.shuo.dsa.sort;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 字典排序
 */
public class DictComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        char[] o1s = o1.toCharArray();
        char[] o2s = o2.toCharArray();
        int len = Math.min(o1s.length, o2s.length);
        for (int i = 0; i < len; i++) {
            if (o1s[i] > o2s[i]) return 1;
            if (o1s[i] < o2s[i]) return -1;
        }
        if (o1s.length == o2s.length) return 0;
        return o1s.length > o2s.length ? 1 : -1;
    }

    public static void main(String[] args) {
        List<String> ss = new ArrayList<>();
        ss.add("123");
        ss.add("22");
        ss.add("777");
        ss.add("1");
        ss.add("1");
        ss.add("99992");
        ss.sort(new DictComparator());
        System.out.println(ss);
    }
}
